package com.example.ovulationperiodtracker.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.ovulationperiodtracker.R;
import com.example.ovulationperiodtracker.databinding.DialogFirstSetBinding;
import com.example.ovulationperiodtracker.databinding.DialogInputBinding;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.Locale;


public class FirstSetDialog extends Dialog {

    private OnClickYesNoListener onClickYesNoListener;
    private Context context;

    public FirstSetDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DialogFirstSetBinding binding = DialogFirstSetBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();

        // 获取屏幕宽度
        int screenWidth = getContext().getResources().getDisplayMetrics().widthPixels;
        // 设置 Dialog 宽度为屏幕宽度的 80%
        layoutParams.width = (int) (screenWidth * 0.9);
        window.setAttributes(layoutParams);
        window.setBackgroundDrawableResource(R.drawable.bg_dialog_ys);

        setCanceledOnTouchOutside(false);
        setCancelable(false);


//        binding.tvNo.setOnClickListener(v -> {
////            if (onClickYesNoListener != null) {
////                onClickYesNoListener.onClickNo();
////            }
//            dismiss();
//        });

        binding.tvYes.setOnClickListener(v -> {
            if (onClickYesNoListener != null) {
                String jingLength = binding.etJingLength.getText().toString().trim();
                String periodLength = binding.etPeriodLenght.getText().toString().trim();

                if (TextUtils.isEmpty(jingLength) || TextUtils.isEmpty(periodLength)) {
                    Toast.makeText(context, "请输入正确的数字", Toast.LENGTH_SHORT).show();
                    return;
                }

                Date date = binding.dateFirstMestruo.getDate();

                LocalDate localDate = date.toInstant()
                        .atZone(ZoneId.systemDefault())
                        .toLocalDate();

                if (localDate.isAfter(LocalDate.now())) {
                    Toast.makeText(context, "不能在未来的时间添加记录", Toast.LENGTH_SHORT).show();
                    return;
                }


                onClickYesNoListener.onClickYes(localDate, Integer.parseInt(jingLength), Integer.parseInt(periodLength));
                dismiss();
            }

        });
    }

    public FirstSetDialog setOnClickYesNoListener(OnClickYesNoListener onClickYesNoListener) {
        this.onClickYesNoListener = onClickYesNoListener;
        return this;
    }

    public interface OnClickYesNoListener {
        void onClickYes(LocalDate localDate, int jingLength, int periodLength);

        // void onClickNo();
    }
}